package com.yljk.yljkproject.modelcallback;

import android.app.Activity;
import android.content.Context;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alsk.rn.common.CommonRouter;
import com.alsk.rn.common.enumtype.ActivityType;
import com.alsk.rn.common.enumtype.MediaType;
import com.alsk.rn.common.serviceapi.CommonService;
import com.baidu.mobstat.StatService;
import com.shicheng.mediaplayer.media.MediaCallBack;
import com.shicheng.mediaplayer.media.MediaManager;
import com.yilijk.base.utils.ALog;
import com.yilijk.base.utils.ActivityManagerUtils;
import com.yilijk.base.utils.DevicesUtils;

/**
 * Created by SHICHENG
 *
 * KitCommon maven库组件回调
 * KitCommon maven lab callback
 * Time on 2021/11/19
 */
@Route(path = CommonRouter.CommonService)
public class CommonServiceImpl implements CommonService {

    @Override
    public void onActivityLifecycle(ActivityType activityType, Activity activity, String s) {
        switch (activityType){
            case ACTIVITY_CREATE:
                ActivityManagerUtils.addActivities(activity);
                break;
            case ACTIVITY_START:
                StatService.onPageStart(activity,s);
                ALog.i("百度统计onPageStart",s);
                break;
            case ACTIVITY_RESUME:
                StatService.onResume(activity);
                ALog.i("百度统计onResume",activity.getClass().toString());
                break;
            case ACTIVITY_PAUSE:
                StatService.onPause(activity);
                ALog.i("百度统计onPause",activity.getClass().toString());
                break;
            case ACTIVITY_STOP:
                break;
            case ACTIVITY_RESTART:
                StatService.onPageEnd(activity,s);
                ALog.i("百度统计onPageEnd",s);
                break;
            case ACTIVITY_DESTORY:
                ActivityManagerUtils.removeActivities(activity);
                break;
            default:
                break;
        }
    }

    @Override
    public void onMedia(MediaType mediaType,Activity activity,String string) {
        switch (mediaType){
            case MEDIA_STOP:
                MediaManager.stop();
                break;
            case MEDIA_START:
                break;
            case MEDIA_PLAY:
                if (MediaManager.getMediaPlayer()!=null){
                    MediaManager.removeAll();
                }
                MediaManager.builder()
                        .setContext(activity)
                        .setUrl(string)
                        .setOpenToast(false)
                        .setDebugModel(DevicesUtils.isEnvironment())
                        .setMediaListener(new MediaCallBack() {
                            @Override
                            public void progress(int i, int max) {

                            }
                            @Override
                            public void prepare(int duration, String time) {
                                MediaManager.start();
                            }
                            @Override
                            public void start() {
                            }
                            @Override
                            public void pause() {

                            }
                            @Override
                            public void stop() {
                            }
                            @Override
                            public void completion() {
                            }
                            @Override
                            public void error() {
                                Toast.makeText(activity, "语音播放出错", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .build();
                break;
            case MEDIA_REMOVE:
                MediaManager.removeAll();
                break;
        }
    }

    @Override
    public String[] onPageGroup() {
        return new String[]{
                "MyActivityDetails",
                "MyActivityList",
                "ImChating",
                "ETPImChating",
                "HelpAndBackPage",
                "Records",
        };
    }

    @Override
    public void init(Context context) {

    }

    @Override
    public String[] onForbiddenBack() {
        return new String[]{
                "Index",
        };
    }
}
